import Observer.EnvironmentData;
import Observer.EnvironmentSensorNetwork;

import java.util.Scanner;

public class EnvironmentMonitor extends SystemManager {
    private EnvironmentSensorNetwork sensorNetwork;

    public EnvironmentMonitor(Scanner scanner, EnvironmentSensorNetwork sensorNetwork) {
        super(scanner);
        this.sensorNetwork = sensorNetwork;
    }

    @Override
    public void manage() {
        while (true) {
            System.out.println("\n=== 农田环境监测 ===");
            System.out.println("1. 模拟环境数据变化");
            System.out.println("2. 查看当前环境数据");
            System.out.println("3. 返回主菜单");

            int choice = getUserChoice(1, 3, "请输入选择(1-3): ");

            if(choice == 3){
                return;
            }

            switch (choice) {
                case 1:
                    simulateEnvironmentChange();
                    break;
                case 2:
                    viewEnvironmentData();
                    break;
                case 3:
                    return;
            }
        }
    }

    private void simulateEnvironmentChange() {
        EnvironmentData data = new EnvironmentData();

        System.out.print("输入土壤湿度(0-1): ");
        data.setSoilMoisture(Double.parseDouble(scanner.nextLine()));

        System.out.print("输入土壤pH值: ");
        data.setSoilPH(Double.parseDouble(scanner.nextLine()));

        System.out.print("输入温度(℃): ");
        data.setTemperature(Double.parseDouble(scanner.nextLine()));

        System.out.printf("输入湿度：");
        data.setHumidity(Double.parseDouble(scanner.nextLine()));

        System.out.printf("输入风速(km/h)：");
        data.setWindSpeed(Double.parseDouble(scanner.nextLine()));

        sensorNetwork.setEnvironmentData(data);
        System.out.println("环境数据已更新并通知所有观察者");
    }

    private void viewEnvironmentData() {
        EnvironmentData data = sensorNetwork.getCurrentData();
        if (data != null) {
            System.out.println("当前环境数据如下：");
            System.out.printf("土壤湿度: %.2f\n", data.getSoilMoisture());
            System.out.printf("土壤pH值: %.2f\n", data.getSoilPH());
            System.out.printf("温度: %.2f ℃\n", data.getTemperature());
            System.out.printf("湿度: %.2f\n", data.getHumidity());
            System.out.printf("风速: %.2f\n", data.getWindSpeed());
        } else {
            System.out.println("尚未设置环境数据。");
        }
    }
}
